<template>
  <div class="leaderboard">
    <!-- 排行榜头部 -->
    <div class="leaderboard-header">
      <div class="header-content">
        <h1>编程排行榜</h1>
        <p>展示平台上最优秀的编程高手</p>
      </div>
    </div>

    <!-- 筛选选项 -->
    <div class="filter-section">
      <div class="filter-tabs">
        <el-radio-group v-model="activeTab" @change="handleTabChange">
          <el-radio-button label="overall">总排行</el-radio-button>
          <el-radio-button label="weekly">周排行</el-radio-button>
          <el-radio-button label="monthly">月排行</el-radio-button>
        </el-radio-group>
      </div>
      
      <div class="filter-options">
        <el-select v-model="categoryFilter" placeholder="分类" @change="handleFilter">
          <el-option label="全部分类" value="" />
          <el-option label="算法" value="algorithm" />
          <el-option label="数据结构" value="data_structure" />
          <el-option label="动态规划" value="dynamic_programming" />
          <el-option label="图论" value="graph" />
        </el-select>
        
        <el-select v-model="difficultyFilter" placeholder="难度" @change="handleFilter">
          <el-option label="全部难度" value="" />
          <el-option label="简单" value="easy" />
          <el-option label="中等" value="medium" />
          <el-option label="困难" value="hard" />
        </el-select>
      </div>
    </div>

    <!-- 前三名展示 -->
    <div class="top-three">
      <div class="podium">
        <!-- 第二名 -->
        <div class="podium-item second">
          <div class="rank-number">2</div>
          <div class="user-avatar">
            <img :src="topUsers[1]?.avatar || '/images/default-avatar.png'" :alt="topUsers[1]?.username" />
          </div>
          <div class="user-info">
            <div class="username">{{ topUsers[1]?.username }}</div>
            <div class="score">{{ topUsers[1]?.score }}分</div>
            <div class="solved">{{ topUsers[1]?.solvedCount }}题</div>
          </div>
        </div>
        
        <!-- 第一名 -->
        <div class="podium-item first">
          <div class="crown">👑</div>
          <div class="rank-number">1</div>
          <div class="user-avatar">
            <img :src="topUsers[0]?.avatar || '/images/default-avatar.png'" :alt="topUsers[0]?.username" />
          </div>
          <div class="user-info">
            <div class="username">{{ topUsers[0]?.username }}</div>
            <div class="score">{{ topUsers[0]?.score }}分</div>
            <div class="solved">{{ topUsers[0]?.solvedCount }}题</div>
          </div>
        </div>
        
        <!-- 第三名 -->
        <div class="podium-item third">
          <div class="rank-number">3</div>
          <div class="user-avatar">
            <img :src="topUsers[2]?.avatar || '/images/default-avatar.png'" :alt="topUsers[2]?.username" />
          </div>
          <div class="user-info">
            <div class="username">{{ topUsers[2]?.username }}</div>
            <div class="score">{{ topUsers[2]?.score }}分</div>
            <div class="solved">{{ topUsers[2]?.solvedCount }}题</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 排行榜列表 -->
    <div class="leaderboard-list">
      <div class="list-header">
        <h3>完整排行榜</h3>
        <span class="total-count">共 {{ filteredUsers.length }} 位用户</span>
      </div>
      
      <div class="users-table">
        <el-table :data="paginatedUsers" style="width: 100%">
          <el-table-column prop="rank" label="排名" width="80" align="center">
            <template #default="{ row }">
              <div class="rank-cell">
                <span class="rank-number" :class="getRankClass(row.rank)">
                  {{ row.rank }}
                </span>
              </div>
            </template>
          </el-table-column>
          
          <el-table-column prop="username" label="用户" min-width="200">
            <template #default="{ row }">
              <div class="user-cell">
                <div class="user-avatar">
                  <img :src="row.avatar || '/images/default-avatar.png'" :alt="row.username" />
                </div>
                <div class="user-details">
                  <div class="username">{{ row.username }}</div>
                  <div class="user-title" v-if="row.title">{{ row.title }}</div>
                </div>
              </div>
            </template>
          </el-table-column>
          
          <el-table-column prop="score" label="总分" width="100" align="center">
            <template #default="{ row }">
              <span class="score-value">{{ row.score }}</span>
            </template>
          </el-table-column>
          
          <el-table-column prop="solvedCount" label="解决题目" width="100" align="center">
            <template #default="{ row }">
              <span class="solved-count">{{ row.solvedCount }}</span>
            </template>
          </el-table-column>
          
          <el-table-column prop="acceptanceRate" label="通过率" width="100" align="center">
            <template #default="{ row }">
              <span class="acceptance-rate">{{ row.acceptanceRate }}%</span>
            </template>
          </el-table-column>
          
          <el-table-column prop="lastActive" label="最后活跃" width="120" align="center">
            <template #default="{ row }">
              <span class="last-active">{{ formatDate(row.lastActive) }}</span>
            </template>
          </el-table-column>
          
          <el-table-column label="操作" width="120" align="center">
            <template #default="{ row }">
              <el-button size="small" @click="viewProfile(row)">
                查看资料
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      
      <!-- 分页 -->
      <div class="pagination-container" v-if="filteredUsers.length > 0">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[20, 50, 100]"
          :total="filteredUsers.length"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 我的排名 -->
    <div class="my-ranking" v-if="currentUser">
      <div class="ranking-card">
        <div class="card-header">
          <h3>我的排名</h3>
        </div>
        <div class="card-content">
          <div class="my-rank">
            <div class="rank-info">
              <span class="rank-label">当前排名</span>
              <span class="rank-value">#{{ currentUser.rank }}</span>
            </div>
            <div class="score-info">
              <span class="score-label">总分</span>
              <span class="score-value">{{ currentUser.score }}</span>
            </div>
          </div>
          <div class="my-stats">
            <div class="stat-item">
              <span class="stat-label">解决题目</span>
              <span class="stat-value">{{ currentUser.solvedCount }}</span>
            </div>
            <div class="stat-item">
              <span class="stat-label">通过率</span>
              <span class="stat-value">{{ currentUser.acceptanceRate }}%</span>
            </div>
            <div class="stat-item">
              <span class="stat-label">提交次数</span>
              <span class="stat-value">{{ currentUser.submissions }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()

// 响应式数据
const activeTab = ref('overall')
const categoryFilter = ref('')
const difficultyFilter = ref('')
const currentPage = ref(1)
const pageSize = ref(20)

// 当前用户信息
const currentUser = ref({
  rank: 156,
  score: 2450,
  solvedCount: 89,
  acceptanceRate: 78,
  submissions: 234
})

// 排行榜数据
const users = ref([
  {
    rank: 1,
    username: 'CodeMaster',
    avatar: '/images/avatar1.jpg',
    title: '算法大师',
    score: 8950,
    solvedCount: 456,
    acceptanceRate: 92,
    lastActive: new Date('2024-01-15')
  },
  {
    rank: 2,
    username: 'AlgoExpert',
    avatar: '/images/avatar2.jpg',
    title: '编程专家',
    score: 8720,
    solvedCount: 423,
    acceptanceRate: 89,
    lastActive: new Date('2024-01-14')
  },
  {
    rank: 3,
    username: 'DataStructureKing',
    avatar: '/images/avatar3.jpg',
    title: '数据结构之王',
    score: 8456,
    solvedCount: 398,
    acceptanceRate: 87,
    lastActive: new Date('2024-01-13')
  },
  // 添加更多用户数据...
  ...Array.from({ length: 97 }, (_, i) => ({
    rank: i + 4,
    username: `User${i + 4}`,
    avatar: `/images/avatar${(i % 10) + 1}.jpg`,
    title: i % 5 === 0 ? '编程高手' : null,
    score: Math.floor(Math.random() * 5000) + 3000,
    solvedCount: Math.floor(Math.random() * 200) + 50,
    acceptanceRate: Math.floor(Math.random() * 40) + 60,
    lastActive: new Date(Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000)
  }))
])

// 计算属性
const topUsers = computed(() => {
  return users.value.slice(0, 3)
})

const filteredUsers = computed(() => {
  let filtered = users.value

  // 这里可以根据 categoryFilter 和 difficultyFilter 进行筛选
  // 目前返回所有用户

  return filtered
})

const paginatedUsers = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value
  const end = start + pageSize.value
  return filteredUsers.value.slice(start, end)
})

// 方法
const getRankClass = (rank) => {
  if (rank <= 3) return 'top-three'
  if (rank <= 10) return 'top-ten'
  return ''
}

const formatDate = (date) => {
  const now = new Date()
  const diff = now - date
  const days = Math.floor(diff / (1000 * 60 * 60 * 24))
  
  if (days === 0) {
    return '今天'
  } else if (days === 1) {
    return '昨天'
  } else if (days < 7) {
    return `${days}天前`
  } else {
    return date.toLocaleDateString('zh-CN')
  }
}

const handleTabChange = (tab) => {
  activeTab.value = tab
  // 根据选择的时间范围重新加载数据
  ElMessage.info(`切换到${tab === 'overall' ? '总排行' : tab === 'weekly' ? '周排行' : '月排行'}`)
}

const handleFilter = () => {
  currentPage.value = 1
  // 根据筛选条件重新加载数据
}

const handleSizeChange = (size) => {
  pageSize.value = size
  currentPage.value = 1
}

const handleCurrentChange = (page) => {
  currentPage.value = page
}

const viewProfile = (user) => {
  ElMessage.info(`查看用户资料：${user.username}`)
}

onMounted(() => {
  // 初始化数据
})
</script>

<style lang="scss" scoped>
.leaderboard {
  min-height: 100vh;
  background: var(--el-bg-color-page);
}

.leaderboard-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 60px 0;
  text-align: center;
  
  .header-content {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
    
    h1 {
      font-size: 3rem;
      font-weight: 700;
      margin-bottom: 16px;
    }
    
    p {
      font-size: 1.25rem;
      opacity: 0.9;
    }
  }
}

.filter-section {
  max-width: 1200px;
  margin: -30px auto 40px;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  
  @media (max-width: 768px) {
    flex-direction: column;
    gap: 16px;
  }
}

.filter-options {
  display: flex;
  gap: 12px;
  
  .el-select {
    width: 120px;
  }
}

.top-three {
  max-width: 1200px;
  margin: 0 auto 60px;
  padding: 0 20px;
}

.podium {
  display: flex;
  justify-content: center;
  align-items: end;
  gap: 20px;
  
  @media (max-width: 768px) {
    flex-direction: column;
    align-items: center;
  }
}

.podium-item {
  background: white;
  border-radius: 16px;
  padding: 30px 20px;
  text-align: center;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  position: relative;
  transition: transform 0.3s ease;
  
  &:hover {
    transform: translateY(-4px);
  }
  
  &.first {
    order: 2;
    transform: scale(1.1);
    background: linear-gradient(135deg, #ffd700 0%, #ffed4e 100%);
    
    .crown {
      position: absolute;
      top: -15px;
      left: 50%;
      transform: translateX(-50%);
      font-size: 24px;
    }
  }
  
  &.second {
    order: 1;
    background: linear-gradient(135deg, #c0c0c0 0%, #e8e8e8 100%);
  }
  
  &.third {
    order: 3;
    background: linear-gradient(135deg, #cd7f32 0%, #daa520 100%);
  }
  
  .rank-number {
    font-size: 24px;
    font-weight: 700;
    margin-bottom: 16px;
    color: white;
  }
  
  .user-avatar {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    margin: 0 auto 16px;
    overflow: hidden;
    border: 4px solid white;
    
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }
  
  .user-info {
    .username {
      font-size: 18px;
      font-weight: 600;
      margin-bottom: 8px;
      color: white;
    }
    
    .score {
      font-size: 16px;
      font-weight: 500;
      margin-bottom: 4px;
      color: white;
    }
    
    .solved {
      font-size: 14px;
      color: rgba(255, 255, 255, 0.8);
    }
  }
}

.leaderboard-list {
  max-width: 1200px;
  margin: 0 auto 40px;
  padding: 0 20px;
}

.list-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  
  h3 {
    font-size: 24px;
    font-weight: 600;
    color: var(--el-text-color-primary);
  }
  
  .total-count {
    font-size: 14px;
    color: var(--el-text-color-secondary);
  }
}

.users-table {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
}

.rank-cell {
  .rank-number {
    font-weight: 600;
    
    &.top-three {
      color: var(--el-color-warning);
      font-size: 18px;
    }
    
    &.top-ten {
      color: var(--el-color-primary);
      font-weight: 700;
    }
  }
}

.user-cell {
  display: flex;
  align-items: center;
  gap: 12px;
  
  .user-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    overflow: hidden;
    
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }
  
  .user-details {
    .username {
      font-weight: 500;
      margin-bottom: 2px;
      color: var(--el-text-color-primary);
    }
    
    .user-title {
      font-size: 12px;
      color: var(--el-color-primary);
    }
  }
}

.score-value {
  font-weight: 600;
  color: var(--el-color-primary);
}

.solved-count {
  font-weight: 500;
  color: var(--el-text-color-primary);
}

.acceptance-rate {
  font-weight: 500;
  color: var(--el-color-success);
}

.last-active {
  font-size: 12px;
  color: var(--el-text-color-secondary);
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.my-ranking {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.ranking-card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  
  .card-header {
    margin-bottom: 20px;
    
    h3 {
      font-size: 18px;
      font-weight: 600;
      color: var(--el-text-color-primary);
    }
  }
  
  .card-content {
    display: flex;
    gap: 40px;
    
    @media (max-width: 768px) {
      flex-direction: column;
      gap: 20px;
    }
  }
  
  .my-rank {
    display: flex;
    gap: 30px;
    
    .rank-info,
    .score-info {
      text-align: center;
      
      .rank-label,
      .score-label {
        display: block;
        font-size: 14px;
        color: var(--el-text-color-secondary);
        margin-bottom: 8px;
      }
      
      .rank-value,
      .score-value {
        font-size: 24px;
        font-weight: 700;
        color: var(--el-color-primary);
      }
    }
  }
  
  .my-stats {
    display: flex;
    gap: 30px;
    
    @media (max-width: 768px) {
      justify-content: space-around;
    }
    
    .stat-item {
      text-align: center;
      
      .stat-label {
        display: block;
        font-size: 12px;
        color: var(--el-text-color-secondary);
        margin-bottom: 4px;
      }
      
      .stat-value {
        font-size: 16px;
        font-weight: 600;
        color: var(--el-text-color-primary);
      }
    }
  }
}

@media (max-width: 768px) {
  .podium {
    .podium-item {
      &.first {
        transform: none;
        order: 1;
      }
      
      &.second {
        order: 2;
      }
      
      &.third {
        order: 3;
      }
    }
  }
}
</style>
